/**
 * 说明：console控制台输出信息格式化
 * 作者：江湖一红
 * 时间：2020/6/11
 */

"use strict"
//创建对象
var $$ = {
    default: "#484848", //默认颜色
    info: "#1475b2", //信息颜色
    success: "green", //成功颜色
    error: "red", //错误颜色
    warning: "#fa0", //警告颜色
    center: "#696969" //中间元素颜色
};

//颜色样式
$$.c = "color:#fff;background:" + $$.default+";padding:3px 8px;border-radius: 4px;"; //css样式  提示样式
$$.cinfo = "color:#fff;background:" + $$.info + ";padding:3px 8px;border-radius: 4px;"; //css样式  信息样式
$$.csuccess = "color:#fff;background:" + $$.success + ";padding:3px 8px;border-radius: 4px;"; //css样式  成功信息样式
$$.cerror = "color:#fff;background:" + $$.error + ";padding:3px 8px;;border-radius: 4px;"; //css样式  错误信息样式
$$.cwarn = "color:#fff;background:" + $$.warning + ";padding:3px 8px;border-radius: 4px;"; //css样式  警告信息样式
$$.cc = "color:#fff;background:" + $$.center + ";padding:3px 8px"; //css样式  中间提示样式
$$.cl = "color:#fff;background:" + $$.default+";padding:3px 8px;border-top-left-radius: 4px;border-bottom-left-radius: 4px;"; //css样式  左侧提示样式
$$.cr = "color:#fff;background:" + $$.default+";padding:3px 8px;border-top-right-radius: 4px;border-bottom-right-radius: 4px;"; //css样式  右侧提示样式
$$.crinfo = "color:#fff;background:" + $$.info + ";padding:3px 8px;border-top-right-radius: 4px;border-bottom-right-radius: 4px;"; //css样式  右侧信息样式
$$.crsuccess = "color:#fff;background:" + $$.success + ";padding:3px 8px;border-top-right-radius: 4px;border-bottom-right-radius: 4px;"; //css样式  右侧成功信息样式
$$.crerror = "color:#fff;background:" + $$.error + ";padding:3px 8px;border-top-right-radius: 4px;border-bottom-right-radius: 4px;"; //css样式  右侧错误信息样式
$$.crwarn = "color:#fff;background:" + $$.warning + ";padding:3px 8px;border-top-right-radius: 4px;border-bottom-right-radius: 4px;"; //css样式  右侧警告信息样式


/**
 * %s 占位符
 * %d 或 %i 整数
 * %f 浮点数
 * %o%O object对象
 * %c css样式
 */

//清空
$$.clear = function() {
    console.clear();
};

//创建输出
$$.console = function(...obj) { //数组  信息类型 格式：fn(["asd","asd"],"info")
    let _s = ``;
    let _info = obj[1] ? obj[1] : "info";
    if (!obj.length) {
        console.log(`%c请输入相关信息！`, $$.cwarn);
    } else {
        if (obj.length == 1) {
            if (Object.prototype.toString.call(obj[0]) == "[object Array]") {
                console.log(`%c${obj[0][0]}`, style(obj[0][0], 0));
            } else {
                console.log(`%c${obj[0]}`, style(obj[0], 0));
            }
        } else {
            if (Object.prototype.toString.call(obj[0]) != "[object Array]") {
                console.log(`%c${obj.toString()}`, style(obj.toString(), 0));
            } else {
                let _l = obj[0].length;
                obj = obj[0];
                for (let i = 0; i < _l; i++) {
                    _s = formatString(obj);
                };
                switch (_l) {
                    case 1:
                        console.log(_s, style(_info, 0), obj[0]);
                        break;
                    case 2:
                        console.log(_s, $$.cl, obj[0], style(_info, 1), obj[1]);
                        break;
                    case 3:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], style(_info, 1), obj[2]);
                        break;
                    case 4:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], $$.cc, obj[2], style(_info, 1), obj[3]);
                        break;
                    case 5:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], $$.cc, obj[2], $$.cc, obj[3], style(_info, 1), obj[4]);
                        break;
                    case 6:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], $$.cc, obj[2], $$.cc, obj[3], $$.cc, obj[3], style(_info, 1), obj[5]);
                        break;
                    case 7:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], $$.cc, obj[2], $$.cc, obj[3], $$.cc, obj[4], $$.cc, obj[5], style(_info, 1), obj[6]);
                        break;
                    case 8:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], $$.cc, obj[2], $$.cc, obj[3], $$.cc, obj[4], $$.cc, obj[5], $$.cc, obj[6], style(_info, 1), obj[7]);
                        break;
                    case 9:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], $$.cc, obj[2], $$.cc, obj[3], $$.cc, obj[4], $$.cc, obj[5], $$.cc, obj[6], $$.cc, obj[7], style(_info, 1), obj[8]);
                        break;
                    case 10:
                        console.log(_s, $$.cl, obj[0], $$.cc, obj[1], $$.cc, obj[2], $$.cc, obj[3], $$.cc, obj[4], $$.cc, obj[5], $$.cc, obj[5], $$.cc, obj[6], _cc, obj[7], $$.cc, obj[8], style(_info, 1), obj[9]);
                        break;
                }
            }
        }
    }

};

//格式化字符串
function formatString(obj) {
    let _s = ``; //占位符字符串
    let _l = obj.length; //长度
    for (let i = 0; i < _l; i++) {
        _s += `%c${placeholder(obj[i])}`;
    }
    return _s;
};

//返回占位符的格式
function placeholder(string) {
    let _s = "%s"; //占位符
    let _string = Object.prototype.toString.call(string); //判断对象类型
    switch (_string) {
        case "[object String]":
            _s = "%s"; //占位符
            break;
        case "[object Number]":
            _s = "%d"; //占位符
            break;
        case "[object Array]":
            _s = "%o"; //占位符
            break;
        case "[object Object]":
            _s = "%o"; //占位符
            break;
    };
    return _s;
};

//返回样式
function style(info, flag) {
    let _obj = ``;
    if (flag == 0) {
        switch (info) {
            case "info":
                _obj = $$.cinfo;
                break;
            case "success":
                _obj = $$.csuccess;
                break;
            case "error":
                _obj = $$.cerror;
                break;
            case "warning":
                _obj = $$.cwarn;
                break;
            default:
                _obj = $$.c;
                break;
        }
    } else {
        switch (info) {
            case "info":
                _obj = $$.crinfo;
                break;
            case "success":
                _obj = $$.crsuccess;
                break;
            case "error":
                _obj = $$.crerror;
                break;
            case "warning":
                _obj = $$.crwarn;
                break;
            default:
                _obj = $$.cr;
                break;
        }
    }
    return _obj;
}